package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Setmeal;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealDetailVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * C端套餐控制器
 */
@Slf4j
@RequestMapping("/user/setmeal")
@RestController("userSetmealController")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据分类id等条件查询套餐
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @Cacheable(value = "setmeal", key = "'categoryId::'+#categoryId")
    public Result<List<Setmeal>> list(Long categoryId) {
        // 记录日志
        log.info("根据分类id等条件查询套餐，categoryId：{}", categoryId);

        // 组织查询条件
        Setmeal setmeal = Setmeal.builder()
                .categoryId(categoryId)
                // 只查询启售的套餐
                .status(StatusConstant.ENABLE)
                .build();

        // 调用service查询获取返回值，并给出响应
        return setmealService.listByCondition(setmeal);
    }

    /**
     * 根据套餐id查询包含的套餐详情
     *
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    @Cacheable(value = "setmeal", key = "'setmealId::' + #id + '::setmealDetail'")
    public Result<List<SetmealDetailVO>> dishList(@PathVariable("id") Long id) {
        return setmealService.listSemealDetailsBySetmealId(id);
    }
}
